package superConference;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


//Creating JFrame
public class GUI360 extends JFrame {
 
	/**
	 * Set serialVersionUID to avoid warning
	 */
	private static final long serialVersionUID = 8904641527445622030L;
	/**
	 * The default width for text fields and text areas.
	 */
	private static final int TEXT_WIDTH = 20;
	/**
	 * The default width for ID numbers.
	 */
	private static final int ID_WIDTH = 6;
	
	private static JTextArea my_action_area = new JTextArea(20, 100);
	private static JFrame my_confFrame;
    private static final JPanel my_display_panel = new JPanel();
    private static final JPanel my_action_panel = new JPanel();
	private static final JTextField my_name_Text = new JTextField(TEXT_WIDTH);
	private static final JTextField my_userID_Text = new JTextField(ID_WIDTH);
	private static final JTextField my_paperID_Text = new JTextField(ID_WIDTH);
	
	private static File my_file = new File("");
	private static AbstractUser my_user_object;

	private static final JButton my_remove_button = new JButton("Close");
	private static final JButton my_closeButton = new JButton("Close");
	
	private static JMenuBar my_mb;
	private static UserManager my_user_manager;
	private static User my_user;
	private static final JMenu my_chair = new JMenu("Chair");
	private static final JMenu my_subChair = new JMenu("Sub Chair");
	private static final JMenu my_reviewer = new JMenu("Reviewer");
	private static final JMenu my_author = new JMenu("Author");
	
	public  GUI360() throws IOException {
		my_user_manager = new UserManager();

		my_confFrame = new JFrame();
		my_confFrame.setSize(1360, 760);
		my_confFrame.setTitle("Conferences for the Multinational Society for Engineering Education");
		// read image file in my HardDisk using imageIO and assign it to buffered image reference 
		URL url = getClass().getResource("/superConference/Conference.jpg");
		BufferedImage bf = ImageIO.read(url);
	
		// adding created component to the JFrame using background image  class
		my_confFrame.setContentPane(new BackgroundImageFrame(bf));

		// Menu
		my_mb = new JMenuBar(); // Menu bar
		final JMenu mnuLogin = new JMenu("Login"); // File Entry on Menu bar
		final JMenuItem mnuItemLogin = new JMenuItem("Login"); // Quit sub item
		final JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item

		final JMenuItem acceptanceList = new JMenuItem("View List of papers" +
													" with acceptance status");
		final JMenuItem acceptanceDecision = new JMenuItem("Make acceptance decision");
		final JMenuItem designateSub = new JMenuItem("Designate Sub Chair");
		final JMenuItem subPaperList = new JMenuItem("Sub Chair paper list");

		final JMenuItem assignPaper = new JMenuItem("Assign paper to reviewer");
		final JMenuItem submitRecomendation = new JMenuItem("Submit Recommendation");

		final JMenuItem submitReview = new JMenuItem("Submit Review");

		final JMenuItem submitPaper = new JMenuItem("Submit Paper");
		final JMenuItem getReviews = new JMenuItem("Get Reviews");
		final JMenuItem updatePaper = new JMenuItem("Update Paper");

		final JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
		final JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry
	
		// Set menu bar
		my_confFrame.setJMenuBar(my_mb);
		//Build Menus
		mnuLogin.add(mnuItemLogin); // Create Login line
		mnuLogin.add(mnuItemQuit); // Create Quit line

		my_chair.add(acceptanceList);
		my_chair.add(acceptanceDecision);
		my_chair.add(designateSub);
		my_chair.add(subPaperList);

		my_subChair.add(assignPaper);
		my_subChair.add(submitRecomendation);

		my_reviewer.add(submitReview);

		my_author.add(submitPaper);
		my_author.add(getReviews);
		my_author.add(updatePaper);

		
		// Create About line
		mnuHelp.add(mnuItemAbout); 
		// Add Menu items to form
		my_mb.add(mnuLogin); 
		my_mb.add(my_chair);
		my_mb.add(my_subChair);
		my_mb.add(my_reviewer);
		my_mb.add(my_author);
		my_mb.add(mnuHelp);

		my_chair.setEnabled(false);
		my_subChair.setEnabled(false);
		my_reviewer.setEnabled(false);
		my_author.setEnabled(false);

	    //Setup display panel items  
	    my_display_panel.setLayout(new BorderLayout());
		my_remove_button.addActionListener(new ListenRemoveButton());
		my_closeButton.addActionListener(new ListenClose());

	    my_confFrame.setLayout(new BorderLayout());
		//Add Menu listeners
		mnuItemLogin.addActionListener(new ListenMenuLogin());
		mnuItemQuit.addActionListener(new ListenMenuQuit());
		acceptanceList.addActionListener(new ListenListWithStatus());
		acceptanceDecision.addActionListener(new ListenDecision());
		designateSub.addActionListener(new ListenDesignateSub());
		subPaperList.addActionListener(new ListenSubPaperList());
		assignPaper.addActionListener(new ListenAssignReviewer());
		submitRecomendation.addActionListener(new ListenSubmitRecommendation());
		submitReview.addActionListener(new ListenSubmitReview());
		submitPaper.addActionListener(new ListenUpdatePaper());
		getReviews.addActionListener(new ListenGetReviews());
		updatePaper.addActionListener(new ListenUpdatePaper());
		mnuItemAbout.addActionListener(new ListenMenuAbout());
		
		// Allows the Swing App to be closed
		my_confFrame.addWindowListener(new ListenCloseWdw());
		my_confFrame.setVisible(true);
	}
	
	private class ListenMenuLogin implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//Setup Login panel
			my_user = null;
			my_chair.setEnabled(false);
			my_subChair.setEnabled(false);
			my_reviewer.setEnabled(false);
			my_author.setEnabled(false);
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
		    final JLabel input_label = new JLabel("Please enter you email address: ");
			final JButton loginButton = new JButton("Submit"); 
			my_action_panel.add(input_label);
			my_action_panel.add(my_name_Text);
			my_action_panel.add(loginButton);
			my_action_panel.add(my_closeButton);
			loginButton.addActionListener(new LoginListener());
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			my_confFrame.setJMenuBar(my_mb);
			my_confFrame.setVisible(true);
		}
	}	

	private class LoginListener implements ActionListener{
		public void actionPerformed(ActionEvent e){			
			final String name = my_name_Text.getText();
		      if (name.length() == 0) {
		        JOptionPane.showMessageDialog(my_confFrame, "You must enter a email address!");
		      }
		      else if((my_user = my_user_manager.login(name)) != null) {
		    	  my_action_panel.removeAll();
		    	  my_action_panel.setSize(500, 50);
				  final JLabel welcome_label = new JLabel("Welcome back " +
						  					my_user.getName());
				  if(my_user_manager.getProgramChairList().contains(my_user)){
					  my_chair.setEnabled(true);
				  }
				  if(my_user_manager.getSubChairList().contains(my_user)){
					  my_subChair.setEnabled(true);
				  }
				  if(my_user_manager.getReviewerList().contains(my_user)){
					  my_reviewer.setEnabled(true);
					  int id = my_user.getUserID();
					  my_user_object = my_user_manager.my_reviewer_list.get(id); //need my_reviewer_list to take in id
				  }																//as key and make protected
				  if(my_user_manager.getAuthorList().contains(my_user)){
					  my_author.setEnabled(true);
				  }
				  my_action_panel.add(welcome_label);
				  my_confFrame.setJMenuBar(my_mb);
				  my_confFrame.setVisible(true);

		      }else{
		    	  int n = JOptionPane.showConfirmDialog(
		    	            null, "Would you like to register?",
		    	            "Name Not found!",
		    	            JOptionPane.YES_NO_OPTION);

		    	  if(n == 0){
		    	            JOptionPane.showMessageDialog(null, "Registration not available at this time!!");
		    	  }else {
		    	    JOptionPane.showMessageDialog(null, "GOODBYE");

		    	  }  
					    		
		        my_name_Text.setText(null); // clears input field
		      }
		}
	}
	
	private class ListenMenuQuit implements ActionListener{
		public void actionPerformed(ActionEvent e){
			System.exit(0);
		}
	}
	
	
	private class ListenRemoveButton implements ActionListener{
		public void actionPerformed(ActionEvent e){
			my_confFrame.remove(my_display_panel);
			my_confFrame.repaint();
			my_confFrame.setVisible(true);
		}
	}
	
	private class ListenClose implements ActionListener{
		public void actionPerformed(ActionEvent e){
			my_confFrame.remove(my_action_panel);
			my_confFrame.repaint();
			my_confFrame.setVisible(true);
		}
	}
	
	private class ListenListWithStatus implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set acceptance list panel to active
			my_action_area.setText(null);
	//****		Need to change to paper list toString ***///
			my_action_area.setText(my_user_manager.getUserList().toString());
			JScrollPane listPane = new JScrollPane(my_action_area);
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("List of Papers with Acceptance Status");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(listPane, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

		}
	}
	
	private class ListenDecision implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set acceptance Decision panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_paper_label = new JLabel("Please enter paper ID: ");
			final JButton acceptButton = new JButton("Accept");
			final JButton rejectButton = new JButton("Reject");
			acceptButton.addActionListener(new AcceptListener());
			rejectButton.addActionListener(new RejectListener());
			my_action_panel.add(input_paper_label);
			my_action_panel.add(my_paperID_Text);
			my_action_panel.add(acceptButton);
			my_action_panel.add(rejectButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			my_confFrame.setVisible(true);
		}
	}
	
	private class AcceptListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			final String name = my_paperID_Text.getText();
		      if (name.length() == 0)
		      {
		    	  JOptionPane.showMessageDialog(my_confFrame, "You must enter a ID!");
		      }
		      else if(name == "found in db")
		      {
		    	  // Update paper status to accepted
		      }else{
		    	  JOptionPane.showMessageDialog(my_confFrame, "Please enter valid ID");
		    	  my_paperID_Text.setText(null); // clears input field
		      }
		}
	}
	
	private class RejectListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			final String name = my_paperID_Text.getText();
		      if (name.length() == 0)
		      {
		    	  JOptionPane.showMessageDialog(my_confFrame, "You must enter a ID!");
		      }
		      else if(name == "found in db")
		      {
		    	  // Update paper status to rejected
		      }else{
		    	  JOptionPane.showMessageDialog(my_confFrame, "Please enter valid ID");
		    	  my_paperID_Text.setText(null); // clears input field
		      }
		}
	}	
	
	
	private class ListenDesignateSub implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Designate Sub-Chair panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_paper_label = new JLabel("Please enter paper ID: ");
			final JLabel input_sub_chair_id = new JLabel("Please enter sub-chair ID: ");
			final JButton assignButton = new JButton("Assign");
			assignButton.addActionListener(new AssignListener());
			my_action_panel.add(input_paper_label);
			my_action_panel.add(my_paperID_Text);
			my_action_panel.add(input_sub_chair_id);
			my_action_panel.add(my_userID_Text);
			my_action_panel.add(assignButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			my_confFrame.setVisible(true);

		}
	}
	
	private class AssignListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			final String name = my_paperID_Text.getText();
		      if (name.length() == 0)
		      {
		    	  JOptionPane.showMessageDialog(my_confFrame, "You must enter a paper ID!");
		      }
		      else if(name == "found in db")
		      {
					final String subChair = my_paperID_Text.getText();
				      if (subChair.length() == 0)
				      {
				    	  JOptionPane.showMessageDialog(my_confFrame, "You must enter a user ID!");
				      }
				      else if(subChair == "found in db")
				      {
				    	  // assign the paper to the sub-chair
				      }else{
				    	  JOptionPane.showMessageDialog(my_confFrame, "Please enter valid user ID");
				    	  my_paperID_Text.setText(null); // clears input field
				      }
		      }else{
		    	  JOptionPane.showMessageDialog(my_confFrame, "Please enter valid paper ID");
		    	  my_paperID_Text.setText(null); // clears input field
		      }
		}
	}	
	
	
	private class ListenSubPaperList implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Sub-Chair paper list panel to active
			my_action_area.setText(null);
			//We need to be able to call a toString to fill this text area.
			my_action_area.setText("This is the list sub-chairs with assigned papers.");
			JScrollPane listPane = new JScrollPane(my_action_area);
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("List of Papers Organized by Sub-Chair");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(listPane, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

		}
	}
	
	private class ListenAssignReviewer implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Assign paper list panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_paper_label = new JLabel("Please enter paper ID: ");
			final JLabel input_sub_chair_id = new JLabel("Please enter reveiwer ID: ");
			final JButton assignButton = new JButton("Assign");
			assignButton.addActionListener(new AssignListener());
			my_action_panel.add(input_paper_label);
			my_action_panel.add(my_paperID_Text);
			my_action_panel.add(input_sub_chair_id);
			my_action_panel.add(my_userID_Text);
			my_action_panel.add(assignButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			
			my_action_area.setText(null);
			//We need to be able to call a toString to fill this text area.
			my_action_area.setText("This is the list of papers.");
			JScrollPane listPane = new JScrollPane(my_action_area);
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("List of Papers");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(listPane, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);
		}
	}	
	
	private class ListenSubmitRecommendation implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Submit Recommendation panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_paper_label = new JLabel("Please enter paper ID: ");
			final JLabel input_label = new JLabel("Please select your file. ");
			final JButton submitButton = new JButton("Save Recommendation.");
			submitButton.addActionListener(new SubmitListener());
			my_action_panel.add(input_paper_label);
			my_action_panel.add(my_paperID_Text);
			my_action_panel.add(input_label);
			my_action_panel.add(submitButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			
			JFileChooser fileChooser = new JFileChooser();
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("Select Recommendation File");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(fileChooser, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

			my_confFrame.setVisible(true);
		}
	}	
	

	private class ListenSubmitReview implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Submit Review panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_paper_label = new JLabel("Please enter paper ID: ");
			final JLabel input_label = new JLabel("Please select your file. ");
			final JButton submitButton = new JButton("Save Review");
			submitButton.addActionListener(new ReviewSubmitListener());
			my_action_panel.add(input_paper_label);
			my_action_panel.add(my_paperID_Text);
			my_action_panel.add(input_label);
			my_action_panel.add(submitButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.addActionListener(new chooserListener());
			
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("Select Review File");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(fileChooser, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

		}
	}	
	private class chooserListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			if(e.getActionCommand().equals("CancelSelection")) {
				my_display_panel.removeAll();
			} else {
				JFileChooser f = (JFileChooser) e.getSource();
				my_file = f.getSelectedFile();
				my_display_panel.removeAll();
				
				my_action_area.setText(null);
				
				final Reviewer r = (Reviewer)my_user_object;
				String text = "";
				
				try {
					text = r.viewReview(my_file);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				my_action_area.setText(text);
				JScrollPane listPane = new JScrollPane(my_action_area);
				//my_display_panel.removeAll();
			    final JLabel list_label = new JLabel("Review");
				my_display_panel.add(list_label, BorderLayout.NORTH);
				my_display_panel.add(my_remove_button, BorderLayout.EAST);
				my_display_panel.add(listPane, BorderLayout.CENTER);
				my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
				my_confFrame.setVisible(true);
			}
		}
	}
	private class ReviewSubmitListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			final Reviewer r = (Reviewer)my_user_object;
			try {
				r.submitReview(Integer.parseInt(my_userID_Text.getText()), my_file);
				//need to save in our directory here or in Reviewer
			} catch (NumberFormatException e1) {
				System.err.println("No int!");
				e1.printStackTrace();
			} catch (FileNotFoundException e1) {
				System.err.println("File not Found!");
				e1.printStackTrace();
			}

		}
	}
	//  *****Need different submit listeners******
	private class SubmitListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//Save selected file to review directory
			//Add review to paper
		}
	}
	
	private class ListenGetReviews implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Get Reviews panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			my_action_area.setText(null);
			//We need to be able to call a toString to fill this text area.
			my_action_area.setText("This is the list of reviews.");
			JScrollPane listPane = new JScrollPane(my_action_area);
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("List of Reviews");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(listPane, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

		}
	}
	
	private class ListenUpdatePaper implements ActionListener{
		public void actionPerformed(ActionEvent e){
			//set Update Paper panel to active
			my_action_panel.removeAll();
			my_action_panel.setSize(500, 50);
			final JLabel input_label = new JLabel("Please select your file. ");
			final JButton submitButton = new JButton("Save Paper");
			submitButton.addActionListener(new SubmitListener());
			my_action_panel.add(input_label);
			my_action_panel.add(submitButton);
			my_action_panel.add(my_closeButton);
			my_confFrame.add(my_action_panel, BorderLayout.NORTH);
			
			JFileChooser fileChooser = new JFileChooser();
			my_display_panel.removeAll();
		    final JLabel list_label = new JLabel("Select Paper File");
			my_display_panel.add(list_label, BorderLayout.NORTH);
			my_display_panel.add(my_remove_button, BorderLayout.EAST);
			my_display_panel.add(fileChooser, BorderLayout.CENTER);
			my_confFrame.add(my_display_panel, BorderLayout.SOUTH);
			my_confFrame.setVisible(true);

		}
	}
	
	
	
	private class ListenMenuAbout implements ActionListener{
		public void actionPerformed(ActionEvent e){
			JOptionPane.showMessageDialog(my_confFrame, 
					"This is the official Multinational Society for Engineering Education " +
					"conference management site.");
		}
	}	
	

	private class ListenCloseWdw extends WindowAdapter{
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	}
	

	
	 
	public static void main(String[] args) throws IOException {
	 
		GUI360 f = new GUI360();
	}
	 
}